package com.shoe.controller;

import com.shoe.pojo.ShoeOrder;
import com.shoe.pojo.ShoeOrderMoney;
import com.shoe.service.ShoeOrderService;
import com.shoe.vo.OrderVo;
import com.shoe.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
@CrossOrigin
public class ShoeOrderController {
    @Autowired
    ShoeOrderService shoeOrderService;

    @RequestMapping("addorder")
    @ResponseBody
    public int addShoeOrder(@RequestParam String userId,@RequestParam String shoegoodId ,@RequestParam String orderMoney ,@RequestParam String reserve2 ,@RequestParam String reserve3,@RequestParam String orderCount){
        ShoeOrder shoeOrder = new ShoeOrder();
        shoeOrder.setOrderUserId(Long.valueOf(userId));
        shoeOrder.setOrderGoodId(Long.valueOf(shoegoodId));
        shoeOrder.setOrderMoney( Long.valueOf(orderMoney));
        shoeOrder.setOrderCount(Byte.valueOf(orderCount));
        shoeOrder.setReserve2(reserve2);
        shoeOrder.setReserve3(reserve3);
        shoeOrder.setOrderState("未发货");
        Random random = new Random();
        Integer number = random.nextInt(9000000) + 10000000;
        shoeOrder.setOrderNo(String.valueOf(number));
        Date d = new Date();
        shoeOrder.setOrderDate(d);
       int x = shoeOrderService.addOrder(shoeOrder);
       if(x > 0){
           return 1;
       }

        return 0;
    }

    @RequestMapping("selectByOrderId")
    @ResponseBody
    public List<OrderVo> selectAll(@RequestParam String userId){
        List<OrderVo> orderVo =  shoeOrderService.selectbyId(Long.valueOf(userId));
        return orderVo;
    }

//    @RequestMapping("selectByOrderId")
//    @ResponseBody
//    public List<ShoeOrder> selectByNo(String orderNo){
//        List<ShoeOrder> order =  shoeOrderService.selectByNo(orderNo);
//        return order;
//    }



    @RequestMapping("orderAll")
    @ResponseBody
    public ResponseResult selectPage(HttpServletRequest request){
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        List<ShoeOrder> list = shoeOrderService.selectAll();
        List<ShoeOrder> shoeOrderList = shoeOrderService.selectPage((page-1)*limit,limit);
        ResponseResult responseResult = new ResponseResult();
        if (shoeOrderList != null){
            responseResult.setCode(0);
            responseResult.setMsg("查询成功");
            responseResult.setCount(list.size());
            responseResult.setData(shoeOrderList);
        }else {
            responseResult.setCode(2);
            responseResult.setMsg("查询失败");
        }
        return responseResult;
    }

    @RequestMapping("deleteByorderNo")
    public String deleteOrder(@RequestParam String orderNo){
       int a = shoeOrderService.deleteByorderNo(orderNo);

       if(a == 1){
           return "redirect:http://localhost:63343/qiguai/html/orderpush.html?2";
       }
        return null;
    }


    @GetMapping("updateOrder")
    @ResponseBody
    public ResponseResult updateOrder(Integer orderId,String orderState){
        ShoeOrder shoeOrder = new ShoeOrder();
        shoeOrder.setOrderId(Long.valueOf(orderId));
        shoeOrder.setOrderState(orderState);
        int a = shoeOrderService.updateOrder(shoeOrder);
        ResponseResult responseResult = new ResponseResult();
        if (a !=0){
            responseResult.setCode(0);
            responseResult.setMsg("更新成功");
        }else {
            responseResult.setCode(2);
            responseResult.setMsg("更新失败");
        }
        return responseResult;
    }


    @GetMapping("deleteOrder")
    @ResponseBody
    public ResponseResult deleteOrder(Long orderId){
        int a = shoeOrderService.deleteOrder(orderId);
        ResponseResult responseResult = new ResponseResult();
        if (a !=0){
            responseResult.setCode(0);
            responseResult.setMsg("删除成功");
        }else{
            responseResult.setCode(2);
            responseResult.setMsg("删除失败");
        }
        return responseResult;
    }


    @RequestMapping("selectClassify")
    @ResponseBody
    public ResponseResult selectClassify(String time){
        ShoeOrder shoeOrder = new ShoeOrder();
        List<ShoeOrder> list = shoeOrderService.selectClassify(shoeOrder,time);
        ResponseResult responseResult = new ResponseResult();
        if (list !=null && list.size() >0){
            responseResult.setCode(0);
            responseResult.setData(list);
            return responseResult;
        }else{
            responseResult.setCode(2);

        }
        return responseResult;
    }

    @RequestMapping("selectOrderMoney")
    @ResponseBody
    public List<ShoeOrderMoney> selectOrderMoney(String orderDate1, String orderDate2){
        List<ShoeOrderMoney> list = shoeOrderService.selectOrderMoney(orderDate1,orderDate2);
        return list;
    }
}
